﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.Domain.Entity;
using TR.Domain.Repository.Bs_SupplyRepository;

namespace TR.EFCore.Repository.Bs_SupplyRepository
{
    public class Bs_SupplyRepository : Ibs_SupplyRepository
    {

        private readonly MyContext context;

        public Bs_SupplyRepository  (MyContext context)
        {
            this.context = context;
        }

        private int fyzts;

        public async Task<List<bs_Supply>> SupplyFenYeTjAsync(int pagesize , int current, bs_Supply bs)
        {
            var query = context.bs_Supply.AsQueryable();
            query = query.Where(p => p.is_del == 0);

            if (bs != null)
            {
                if (!string.IsNullOrEmpty(bs.supply_name))
                {
                    query = query.Where(p => EF.Functions.Like(p.supply_name, '%' + bs.supply_name + '%'));
                }
            }
            var result = await query.OrderByDescending(p => p.supply_code).Skip(pagesize * (current - 1)).Take(pagesize).ToListAsync();

            fyzts = await query.CountAsync();

            return result;
                
        }

        public async Task<int> ZtsSelectAsync()
        {
            return fyzts;
        }

        //添加
        public async Task<bool> SupplyAddAsync(bs_Supply bs)
        {
             context.bs_Supply.Add(bs);
            return await context.SaveChangesAsync() > 0 ? true : false;
        }

     

        public async Task<bool> FinanceAddAsync(bs_Finance bsF)
        {
             context.bs_Finance.Add(bsF);
             return await context.SaveChangesAsync() > 0 ? true : false;
        }
    }
}
